package educrypt.gui.components;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSeparator;

import educrypt.gui.view.TextOptionsView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

class TextOptionsPanel extends EducryptPanel implements TextOptionsView {

	private static final long serialVersionUID = 4018602437921131256L;

	private JButton btnAddType, btnAddLanguage;
	private JComboBox cbxType, cbxLanguage;

	private String context;

	public TextOptionsPanel() {

		this.setSize(750, 80);
		this.setPreferredSize(this.getSize());
		setLayout(null);

		JLabel lblTextCharacteristics = new JLabel(
				Messages.getString("guiTextCharacteristics"));
		lblTextCharacteristics.setBounds(30, 11, 272, 19);
		lblTextCharacteristics.setFont(new Font("Tahoma", Font.BOLD, 15));
		add(lblTextCharacteristics);

		JLabel lblType = new JLabel(Messages.getString("guiType"));
		lblType.setBounds(30, 44, 41, 18);
		add(lblType);

		cbxType = new JComboBox();
		cbxType.setEnabled(false);
		cbxType.setBounds(71, 42, 200, 20);
		add(cbxType);

		JLabel lblLanguage = new JLabel(Messages.getString("guiLanguage"));
		lblLanguage.setBounds(393, 44, 70, 18);
		add(lblLanguage);

		ImageIcon iconAdd = new ImageIcon(
				EduCryptResources.getImageURL("plus24.png"));

		btnAddType = new JButton(iconAdd);
		btnAddType.setEnabled(false);
		btnAddType.setBounds(278, 40, 24, 24);
		add(btnAddType);

		btnAddLanguage = new JButton(iconAdd);
		btnAddLanguage.setEnabled(false);
		btnAddLanguage.setBounds(670, 40, 24, 24);
		add(btnAddLanguage);

		cbxLanguage = new JComboBox();
		cbxLanguage.setEnabled(false);
		cbxLanguage.setBounds(463, 42, 200, 20);
		add(cbxLanguage);

		JSeparator separator = new JSeparator();
		separator.setBounds(204, 78, 359, 1);
		add(separator);

		setPanelEnabled(false);
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnAddType.setActionCommand(TYPE_EDITOR);
		btnAddType.addActionListener(ctr);
		btnAddLanguage.setActionCommand(LANGUAGE_EDITOR);
		btnAddLanguage.addActionListener(ctr);
	}

	@Override
	public void showTypes(List<String> types) {
		// cbxType.setEnabled(true);
		// btnAddType.setEnabled(true);
		cbxType.removeAllItems();

		if (this.context.equals(CONTEXT_ANALYSIS)) {
			cbxType.addItem(Messages.getString("guiSelectType"));

		} else if (this.context.equals(CONTEXT_CRYPTANALYSIS)) {
			cbxType.addItem(Messages.getString("guiUnknown"));
		}
		for (String type : types) {
			cbxType.addItem(type);
		}
	}

	@Override
	public void showLanguages(List<String> languages) {
		cbxLanguage.removeAllItems();
		// You must choose a language even in cryptanalysis
		cbxLanguage.addItem(Messages.getString("guiSelectLanguage"));
		for (String lang : languages) {
			cbxLanguage.addItem(lang);
		}
	}

	@Override
	public void setCryptAnalysisContext() {
		this.context = CONTEXT_CRYPTANALYSIS;
	}

	@Override
	public void setAnalysisContext() {
		this.context = CONTEXT_ANALYSIS;
	}

	@Override
	public String getSelectedType() {
		String selectedType;
		if (context.equals(CONTEXT_CRYPTANALYSIS)
				&& cbxType.getSelectedIndex() == 0) {
			selectedType = null;
		} else {
			selectedType = (String) cbxType.getSelectedItem();
		}
		return selectedType;
	}

	@Override
	public String getSelectedLanguage() {
		return (String) cbxLanguage.getSelectedItem();
	}

	@Override
	public int getSelectedTypeIndex() {
		return cbxType.getSelectedIndex();

	}

	@Override
	public int getSelectedLanguageIndex() {
		return cbxLanguage.getSelectedIndex();
	}
}
